package com.ruoyi.system.controller.bm;

import com.alibaba.fastjson2.JSON;
import com.github.binarywang.wxpay.bean.notify.*;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ruoyi.system.service.BaseWxPayCallbackService;
import com.ruoyi.system.service.BaseWxPayService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 微信支付的公共接口
 *
 * @apiFolder 公共/微信支付
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/common/wx-pay")
public class WxPayController {

    private final BaseWxPayCallbackService baseWxPayCallbackService;

    @Resource
    private BaseWxPayService baseWxPayService;

    /**
     * 支付通知回调接口
     *
     * @param request
     * @return
     * @apiStatus released
     */
    @PostMapping("/pay-notify-callback/{indexId}")
    public ResponseEntity<String> payNotifyCallback(@PathVariable("indexId") Integer indexId, @RequestBody String notifyData, HttpServletRequest request) {
        log.info("微信支付回调通知开始, indexId: {}", indexId);
        SignatureHeader header = getRequestHeader(request);
        try {
            WxPayNotifyV3Result res = baseWxPayService.payNotifyCallback(notifyData, header,indexId);
            // 获取微信回调的的XML数据
//            WxPayNotifyV3Result res = wxPayService.parseOrderNotifyV3Result(notifyData, header);
            log.info("微信支付回调通知数据, xml: {}", res);
            // 回调处理
            baseWxPayCallbackService.handlePayNotify(res);
        } catch (Exception e) {
            log.error("微信支付回调通知异常, error: {}", e.getMessage(), e);
            // 异常时返回错误码
            return ResponseEntity.status(500).body(e.getMessage());
        }
        log.info("微信支付回调通知处理成功");
        return ResponseEntity.status(200).body("SUCCESS");
    }

    /**
     * 支付退款回调接口
     *
     * @param request
     * @return
     * @apiStatus released
     */
    @PostMapping("/refund-notify-callback/{indexId}")
    public ResponseEntity<String> refundNotifyCallback(@PathVariable("indexId") Integer indexId,@RequestBody String notifyData, HttpServletRequest request) {
        SignatureHeader header = getRequestHeader(request);
        try {
            WxPayRefundNotifyV3Result res = baseWxPayService.refundNotifyCallback(notifyData, header,indexId);
            // 获取微信回调的的XML数据
//            WxPayRefundNotifyV3Result res = wxPayService.parseRefundNotifyV3Result(notifyData, header);
            log.info("微信支付回调通知数据, xml: {}", res);
            // 回调处理
            baseWxPayCallbackService.handleRefundNotify(res);
        } catch (Exception e) {
            log.error("微信支付回调通知异常, error: {}", e.getMessage(), e);
            // 异常时返回错误码
            return ResponseEntity.status(500).body(e.getMessage());
        }
        log.info("微信支付回调通知处理成功");
        return ResponseEntity.status(200).body("SUCCESS");
    }

    /**
     * 组装请求头重的前面信息
     *
     * @param request
     * @return
     */
    private SignatureHeader getRequestHeader(HttpServletRequest request) {
        // 获取通知签名
        String signature = request.getHeader("Wechatpay-Signature");
        String nonce = request.getHeader("Wechatpay-Nonce");
        String serial = request.getHeader("Wechatpay-Serial");
        String timestamp = request.getHeader("Wechatpay-Timestamp");

        SignatureHeader signatureHeader = new SignatureHeader();
        signatureHeader.setSignature(signature);
        signatureHeader.setNonce(nonce);
        signatureHeader.setSerial(serial);
        signatureHeader.setTimeStamp(timestamp);
        return signatureHeader;
    }
}
